package gemini.pollux.ui.client.page.content.product.product;

import gemini.basic.model.ProductType;
import gemini.pollux.ui.client.ServiceCatalog;
import gemini.pollux.ui.client.mvp.callback.AbstractAsyncCallback;
import gemini.pollux.ui.client.mvp.container.PolluxPlaceContainer;
import gemini.pollux.ui.client.mvp.container.PolluxPlaceContainer.PresenterGetter;
import gemini.pollux.ui.client.mvp.container.PolluxWidgetContainerDisplay;
import gemini.pollux.ui.client.mvp.container.PolluxWidgetContainerPresenter;
import gemini.pollux.ui.client.mvp.gin.PolluxGinjector;
import gemini.pollux.ui.client.page.RootEvent;
import gemini.pollux.ui.client.utils.UIConstants;

import java.util.ArrayList;
import java.util.List;

import ch.elca.gwt.mvp.client.EventBus;
import ch.elca.gwt.mvp.client.place.Place;
import ch.elca.gwt.mvp.client.place.PlaceRequest;

import com.google.inject.Inject;

public class ProductPresenter extends
		PolluxWidgetContainerPresenter<ProductPresenter.Display> {
	
	
	public interface Display extends PolluxWidgetContainerDisplay {
	}

	public static final Place PLACE = new PolluxPlaceContainer<ProductPresenter>(
			UIConstants.PLACE_PRODUCT, new PresenterGetter<ProductPresenter>() {
				public ProductPresenter getPresenter() {
					return PolluxGinjector.INSTANCE.getProductPresenter();
				}
			});

	@Inject
	public ProductPresenter(Display display, EventBus eventBus) {
		super(display, eventBus);
		addPresenter(PolluxGinjector.INSTANCE.getProductListPresenter());
		addPresenter(PolluxGinjector.INSTANCE.getProductEditPresenter());
	}

	/*
	 * Register listeners on Display actions
	 */
	@Override
	protected void onBind() {
		super.onBind();
		
		registerHandlerRegister();
		
		loadProductTypes();
	}
	
	private List<ProductType> productTypes;
	
	private void loadProductTypes() {
		eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.MASK));
		ServiceCatalog.getProductTypeService().getAllProductTypes(
				new AbstractAsyncCallback<ArrayList<ProductType>>() {

					@Override
					public void onSuccess(ArrayList<ProductType> result) {
						eventBus.fireEvent(
								new RootEvent(RootEvent.ForwardType.UN_MASK));
						productTypes = result;
						
						ProductObject prOb = new ProductObject();
						prOb.setProductTypes(productTypes);
						showPresenter(PolluxGinjector.INSTANCE
								.getProductListPresenter(),
								prOb);
					}
				});
	}

	private void registerHandlerRegister() {
		registerHandler(getLocalEventBus().addHandler(ProductEvent.getType(), new ProductHandler() {

			@Override
			public void onBackToList(ProductEvent homeEvent) {
				ProductObject prOb = new ProductObject();
				prOb.setProductTypes(productTypes);
				showPresenter(PolluxGinjector.INSTANCE
						.getProductListPresenter(),
						prOb);
			}

			@Override
			public void onEdit(ProductEvent homeEvent) {
				ProductObject prOb = (ProductObject) getCurrentPresenter().getOutputObject();
				prOb.setProductTypes(productTypes);
				
				showPresenter(PolluxGinjector.INSTANCE
						.getProductEditPresenter(),
						prOb);
			}

			@Override
			public void onNew(ProductEvent levelEvent) {
				ProductObject prOb = new ProductObject();
				prOb.setProductTypes(productTypes);
				
				showPresenter(PolluxGinjector.INSTANCE
						.getProductEditPresenter(),
						prOb);
			}
		}));
	}
	
	@Override
	public Place getPlace() {
		return PLACE;
	}

	@Override
	protected void onPlaceRequest(PlaceRequest request) {
		// TODO Auto-generated method stub
	}
	
	@Override
	public void onUnbind(){
		productTypes = null;
	}
}
